﻿using System;
using System.Collections.Generic;
using System.Text;
using BlueFinity.mvNET.CoreObjects;
using mvLibrary.Helpers.Clases.Segurizacion;
using mvLibrary.Helpers.Exceptions;
using mvLibrary.Helpers.Utils;
using mvLibrary.Helpers.Enums;
using mvLibrary.Helpers.BaseClass;

namespace mvLibrary.Helpers
{
    public class ProgramCaller
    {
        private static ProgramCaller instance;
        private const string TIPO_PANTALLA_INDEX = "20";
        private const int USER_INDEX = 1;
        private const int CONSULTA_INDEX = 5;
        private const string TIPO_PANTALLA_MENU = "3";
        private const string QUERY_PERMISOS_MENU = "SELECT ACC.PANT.ESP WITH 1 EQ \"{0}\" AND WITH {1} EQ \"{2}\" BY 0";
        private const string QUERY_PERMISOS_USUARIO = "SELECT ACC.LPANT.NET WITH 0 EQ \"{0}\" ";
        private const string QUERY_USUARIOS_CONECTADOS = "LISTU";
        private const string QUERY_PERMISOS_POR_PANTALLA = "SELECT ACC.PANT.ESP WITH 0 EQ \"{0}\" AND WITH 1 EQ \"{1}\" AND WITH 20 EQ \"{2}\" ";




        private ProgramCaller()
        {
        }

        public static ProgramCaller Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ProgramCaller();
                }
                return instance;
            }
        }

        public void ReadNet(mvAccount pickAccount, ref string strItem,ref string itemId,ref string strMv,ref string defaultFormule, ref string result)
        {
            pickAccount.CallProg("READ.NET", ref strItem, ref itemId, ref strMv, ref defaultFormule, ref result);
        }

        public void SaveNet(mvAccount pickAccount, ref string strItem, ref string itemId, ref string counterFormule, ref string strItemStatus, ref string strItemIdType, ref string nextId, ref string fileName, ref string user, ref string errorMessage)
        {
            pickAccount.CallProg("SAVE.NET", ref strItem, ref itemId, ref counterFormule, ref strItemStatus, ref strItemIdType, ref nextId, ref fileName, ref user, ref errorMessage);
        }

        public void SaveItem(mvAccount pickAccount,string item, string itemId, string fileName, string user )
        {
            pickAccount.CallProg("SAVE.ITEM",ref item,ref itemId,ref fileName,ref user);
        }

        public void ValidatePassword(mvAccount pickAccount,string userId,string password, ref string errorMessage)
        {
            pickAccount.CallProg("USER.PASSWORD", ref userId, ref password, ref errorMessage);
        }

        public BasicMvItem GetItemById(mvAccount pickAccount,string itemId,string fileName)
        {
            string item = string.Empty;
            pickAccount.CallProg("READ.ITEM.BY.ID",ref item, ref itemId,ref fileName);
            var itemFromPick = new BasicMvItem(item);

            return itemFromPick;
        }
        public string ExecuteSelectNetProgram(mvAccount pickAccount, string fileName, string attributeNumber,
                                              string sortClause, string selectionClause)
        {
            string itemsList = string.Empty;
            pickAccount.CallProg("SELECT.NET", ref fileName, ref attributeNumber, ref sortClause, ref selectionClause,
                                 ref itemsList);

            return itemsList;
        }

        public string ExecuteReportNetProgram(mvAccount pickAccount, string user, string tclCommand, string parameters)
        {
            string report = string.Empty;
            pickAccount.CallProg("REPORT.NET", ref user, ref tclCommand, ref parameters, ref report);

            return report;

        }


        public mvItemList GetPermisosPorUsuarios(mvAccount pickAccount, string userId)
        {
            try
            {
                var tclCommand = string.Format(QUERY_PERMISOS_USUARIO, userId);
                pickAccount.Login();
                mvItemList pickListMenues = pickAccount.Select(tclCommand);
                return pickListMenues;
            }
            catch (mvException ex)
            {
                throw new PermissionsException(ex);
            }
            finally
            {
                pickAccount.Logout();
            }

        }
        /*
         * Este metodo se encarga de buscar en el archivo ACC.PANT.ESP, 
         * los permisos para los distintos menues de la aplicacion,
         * para el usuario que se esta logueando.
         * Luego devuelve un objeto con toda la informacion.
         */
        public mvItemList GetMenuPermissionsFromPick(mvAccount pickAccount, string userId)
        {
            try
            {
                var tclCommand = string.Format(QUERY_PERMISOS_MENU, userId, TIPO_PANTALLA_INDEX, TIPO_PANTALLA_MENU);

                pickAccount.Login();
                return pickAccount.Select(tclCommand);
            }
            catch (mvException ex)
            {
                throw new PermissionsException(ex);
            }
            finally
            {
                pickAccount.Logout();
            }
        }

        public string GetConnectedUsers(mvAccount pickAccount)
        {
            try
            {
                pickAccount.Login();
                string outPut = string.Empty;
                pickAccount.Execute(QUERY_USUARIOS_CONECTADOS, ref outPut);

                return outPut;
            }
            catch (mvException ex)
            {
                throw new PermissionsException(ex);
            }
            finally
            {
                pickAccount.Logout();
            }
        }

        public mvItem GetPermissionsPorPantalla(string pantalla,int tipoPantalla,mvAccount pickAccount,string userId)
        {
            try
            {
                var tclCommand = string.Format(QUERY_PERMISOS_POR_PANTALLA,pantalla, userId, tipoPantalla);
                pickAccount.Login();

                mvItemList list = pickAccount.Select(tclCommand);

                return list.ReadNext();
            }
            catch (mvException ex)
            {
                throw new PermissionsException(ex);
            }
            finally
            {
                pickAccount.Logout();
            }
        }
    }
}
